var apiObject = require('api.js');

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


function request(url, data = {}, header = "application/x-www-form-urlencoded", method = "POST") {
  wx.showLoading({
    title: '加载中...',
  });
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': header,
        'X-token': wx.getStorageSync('token')
      },
      success: function(res) {
        res = res.data;
        wx.hideLoading();
        if (res.code == 10010) {
          showModal("登录身份失效，请重新登录").then(function () {
            wx.redirectTo({
              url: '/pages/login/index',
            })
          })
        } else
          resolve(res);
      },
      fail: function(err) {
        wx.hideLoading();
        reject(err)
      }
    })
  });
}

function uploadImage(url, filePath, formData = {}, header = "application/form-data") {
  wx.showLoading({
    title: '加载中...',
  });
  return new Promise(function(resolve, reject) {
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: "image",
      formData: formData,
      header: {
        'Content-Type': header,
        'X-token': wx.getStorageSync('token')
      },
      success: function (res) {
        res = res.data;
        wx.hideLoading();
        if (res.code == 10010) {
          showModal("登录身份失效，请重新登录").then(function () {
            wx.redirectTo({
              url: '/pages/login/index',
            })
          })
        } else {
          var data = JSON.parse(res);  
          resolve(data);
        }
      },
      fail: function (err) {
        wx.hideLoading();
        reject(err)
      }
    })
  });
}


function showModal(content, showCancel = false, confirmText = "确定") {
  return new Promise(function(resolve, reject) {
    wx.showModal({
      content: content,
      showCancel: showCancel,
      confirmText: confirmText,
      confirmColor: "#008ddd",
      success: function(res) {
        resolve(res)
      }
    })
  });
}

function lineEchartOption(op) {
  var option = {
    color: ["#008ddd"],
    legend: {
      data: [op.title],
      left: 'center',
      borderColor: '#008ddd',
      borderRadius: 5,
      borderWidth: 1,
      top: 20,
      z: 100
    },
    grid: {
      top:"30%",
      bottom:10,
      containLabel: true,
      backgroundColor: "#fff"
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: op.xData,
      // show: false
    },
    yAxis: {
      x: 'center',
      min: op.minLimit,
      max: op.maxLimit,
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    visualMap: {
      top: 0,
      right: 0,
      pieces: [{
        gt: op.minLimit,
        lte: op.min,
        color: '#cc0033'
      }, {
          gt: op.min,
          lte: op.max,
        color: '#008ddd'
      }, {
          gt: op.max,
          lte: op.maxLimit,
        color: '#cc0033'
      }],
      outOfRange: {
        color: '#999'
      }
    },
    tooltip: {
      position: [0, 0]
    },
    series: [{
      name: op.title,
      type: 'line',
      smooth: true,
      data: op.yData,
      markLine: {
        silent: true,
        lineStyle: {
          type: 'solid'
        },
        data: [{
          yAxis: op.min
        }, {
            yAxis: op.max
        }]
      }
    }, ]
  };
  return option;
}


const wxPromisify = fn => {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        resolve(res)
      }

      obj.fail = function (res) {
        reject(res)
      }

      fn(obj)
    })
  }
}
module.exports = {
  formatTime,
  request,
  uploadImage,
  showModal,
  lineEchartOption,
  wxPromisify

}